package com.itheima.controller;

import com.aliyuncs.exceptions.ClientException;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.entity.Result;
import com.itheima.utils.SMSUtils;
import com.itheima.utils.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

@RestController
@RequestMapping("/validateCode")
public class ValiDateCodeController {

    @Autowired
    private JedisPool jedisPool;

    //体检预约短信校验
    @RequestMapping("/send4Order")
    public Result send4Order(String telephone){
        try {
            //产生随机验证码
            Integer integer = ValidateCodeUtils.generateValidateCode(4);
            //短信验证
            SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE,telephone,integer.toString());
            //保存到缓存中有效时间为5分钟
            jedisPool.getResource().setex(telephone+RedisMessageConstant.SENDTYPE_ORDER,600,integer.toString());
            return new Result(true,MessageConstant.SEND_VALIDATECODE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
        }

    }
    //登录短信校验
    @RequestMapping("/send4Login")
    public Result send4Login(String telephone){
        try {
            //产生随机验证码
            Integer integer = ValidateCodeUtils.generateValidateCode(6);
            //短信验证
            SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE,telephone,integer.toString());
            //保存到缓存中有效时间为5分钟
            jedisPool.getResource().setex(telephone+RedisMessageConstant.SENDTYPE_LOGIN,600,integer.toString());
            return new Result(true,MessageConstant.LOGIN_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.VALIDATECODE_ERROR);
        }

    }
}
